﻿using System;
using Dou.URP;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

namespace Volumes
{
    /// <summary>
    /// 常用环境设定
    /// </summary>
    [Serializable, VolumeComponentMenu("VolumeAndFeature/Environment")]
    public class Environment : VolumeComponent, IPostProcessComponent, IVolumeBehaviour
    {
        [Serializable]
        public class FogModeParameter : VolumeParameter<FogMode> {
            public FogModeParameter(FogMode value, bool overrideState = false) : base(value, overrideState) { }
        }
        
        [InspectorName("天空盒材质")]
        public VolumeParameter<Material> skyboxMat = new VolumeParameter<Material>();
        
        [InspectorName("环境雾开关")]
        public BoolParameter useFog = new BoolParameter(false, true);
        [InspectorName("雾模式")]
        public FogModeParameter fogMode = new FogModeParameter(FogMode.Linear, false);
        [InspectorName("雾开始距离")]
        public FloatParameter fogStart = new FloatParameter(70, false);
        [InspectorName("雾结束距离")]
        public FloatParameter fogEnd = new FloatParameter(750, false);
        [InspectorName("雾颜色")]
        public ColorParameter fogColor = new ColorParameter(Color.white, false, false, false);
        
        [InspectorName("环境光颜色")]
        public ColorParameter ambientColor = new ColorParameter(Color.black, true, false, true);
        
        public bool IsActive() => true;
        public bool IsTileCompatible() => true;

        public void OnActivate()
        {
            RenderSettings.skybox = skyboxMat.value;
            
            RenderSettings.fog = useFog.value;
            RenderSettings.fogMode = fogMode.value;
            RenderSettings.fogStartDistance = fogStart.value;
            RenderSettings.fogEndDistance = fogEnd.value;
            RenderSettings.fogColor = fogColor.value;
            
            RenderSettings.defaultReflectionMode = DefaultReflectionMode.Custom;
            
            RenderSettings.ambientMode = AmbientMode.Flat;
            RenderSettings.ambientSkyColor = ambientColor.value;
        }

        public void OnUpdate()
        {
        }

        public void OnDeactive()
        {
        }
    }
}
